﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.IO;
using System.Web.Script.Services;
using System.Web.Services;


namespace TranTekBE.KPI_NhanSu
{
    public partial class Chinh_Sua_Lich_Lam_Viec_Nhan_Vien : PagePopUp
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Load JS
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
            string id_click_event = FunctionsUtils.GetValueStringFromQueryString("idevent", "0");
            if (!IsPostBack)
            {
                // Get user
                FunctionsUtils.GetDropListUser(droplist_ten_nhan_vien);
                // Get stores
                FunctionsUtils.GetDropListStore(droplist_cua_hang_lam_viec);

                //string sql_Id_Event = "SELECT * FROM tbl_LichLamViec WHERE ID_LICH_LAM_VIEC = '"+id_click_event+"'";
                string sql_Id_Event = string.Format( @"
                SELECT * FROM tbl_LichLamViec as Lich
                Left join tbl_UserKPI as KPI
                on Lich.ID_USER = KPI.ID_USER
                Where
                Lich.ID_LICH_LAM_VIEC = '{0}'
                ", id_click_event);
                DataSet Event_user = FunctionsUtils.Database2.ExecuteQuery(sql_Id_Event);
                if (Event_user != null && Event_user.Tables.Count > 0 && Event_user.Tables[0].Rows.Count > 0)
                {
                    lblNgayLamViec.Text = Convert.ToDateTime(Event_user.Tables[0].Rows[0]["NGAY_THEM_VAO"].ToString()).ToShortDateString();
                    int ca_lam_viec = FunctionsUtils.ConvertToInt(Event_user.Tables[0].Rows[0]["CA_LAM_VIEC"],0);
                    radio_btn_ca.SelectedValue = ca_lam_viec.ToString();
                    droplist_cua_hang_lam_viec.Text = Event_user.Tables[0].Rows[0]["CUA_HANG_DANG_LAM_VIEC"].ToString();
                    droplist_ten_nhan_vien.SelectedValue = Event_user.Tables[0].Rows[0]["ID_USER"].ToString();
                    
                }
            }
        }

        protected void btnEditEvent_Click(object sender, EventArgs e)
        {
            int check_id_event = Convert.ToInt32(Request.QueryString["idevent"]);
            string id_nhan_vien = droplist_ten_nhan_vien.SelectedValue.ToString();
            int ca_lam_viec = Convert.ToInt32(radio_btn_ca.SelectedValue.ToString());
            string cua_hang_lam_viec = droplist_cua_hang_lam_viec.SelectedValue.ToString();

            try
            {
                // Update thông tin lịch làm việc người dùng 
                if (id_nhan_vien != null && id_nhan_vien != "" && cua_hang_lam_viec != null && cua_hang_lam_viec != "")
                {
                    string get_user_name = "";
                    string sql_get_user = @"
                    select C001900 as IdUser, C001905+' '+C001904 as NameUser, C001901 as UserName,CONVERT(varchar(max),C001903,2) as Permission,
                    C001909 as Stores
                    from T0019 
                    where C001900 = '" + id_nhan_vien + @"'
                    ";
                    DataSet sql_popup_user = FunctionsUtils.Database.ExecuteQuery(sql_get_user);
                    if (sql_popup_user != null)
                    {
                        //
                        get_user_name = sql_popup_user.Tables[0].Rows[0]["UserName"].ToString();

                    }
                    string sql_update_user_schedule = @" UPDATE tbl_LichLamViec
                            SET
                            ID_USER = '" + id_nhan_vien + @"',
                            NAME_USER = '" + get_user_name + @"',
                            CUA_HANG_DANG_LAM_VIEC = '" + cua_hang_lam_viec + @"', 
                            CA_LAM_VIEC = " + ca_lam_viec + @",
                            NGUOI_THEM_VAO = '" + UserInfo.UserName + @"'
                            WHERE ID_LICH_LAM_VIEC='" + check_id_event + @"'
                            ";
                    FunctionsUtils.Database2.ExecuteNonQuery(sql_update_user_schedule);
                    ClosePopup(true);
                }
                else
                {
                    lbErr.Text = "Lỗi! Vui lòng kiếm tra lại";
                    lbErr.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception err)
            {
                lbErr.Text = err.Message;
                lbErr.BackColor = System.Drawing.Color.Red;
            }
        }// End
    }//
}